<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Area;
use think\facade\View;
use think\exception\ValidateException;
use app\common\model\Address as AddressModel;
use think\response\Json;
use app\common\tool\Result;

class Address extends Base
{
    //获取收货地址列表
    public function index()
    {
        $map = ['uid' => $this->user->id, 'is_del' => 0];
        $list = AddressModel::getList($map, false, 'create_time', ['p', 'c', 'd', 't'],
            'id,nickname,tel,province,city,district,town,details,default');
        foreach ($list as $v) {
            $v->province = $v->p->title;
            $v->city = $v->c->title;
            $v->district = $v->d->title;
           // $v->town = $v->t->title;
            unset($v->p);
            unset($v->c);
            unset($v->d);
            //unset($v->t);
        }
        return Result::success('获取数据成功！', $list);
    }
    //获取收货地址详细内容
    public function edit($id)
    {
        
        $info = AddressModel::with(['p','c','d','t'])->where('id', $id)
            ->field('id,nickname,tel,uid,province,city,district,town,details,default')->find();
        unset($info->province);
        unset($info->city);
        unset($info->district);
        unset($info->town);
        if($this->user->id != $info->uid){
            return Result::fail('数据异常');
        }
        return Result::success('获取数据成功！',$info);
    }
    //修改收货地址
    public function save(): Json
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            $this->validate($param, 'Address');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
        $result = AddressModel::add($param, $this->user);
        $result['code'] = $result['code'] > 1 ? 1 : -1;
        return Result::fastReturn($result);

    }
    //删除收货地址
    public function delete(): Json
    {
        return Result::fastReturn(AddressModel::edit($this->request->post('id'), $this->user));
    }
    // //获取省市区县
    // public function getArea($level = 0)
    // {
    //     $param = $this->request->get();
    //     $code=$param['code'];
    //     $list = Area::getArea($code ?: 100000, $level);
    //     return Result::success('获取数据成功！', $list);
    // }
    
    //获取省市区县
    public function getArea()
    {
        $list = Area::where(['level'=>1])->order('code asc')->field('level,id,province,parent_code,code')->select();
        foreach($list as $k=>$v){
            $v['text']=$v['province'];
            $v['value']=$v['code'];
            $children=Area::where(['level'=>2,'parent_code'=>$v['code']])->field('level,id,city,parent_code,code')->order('code asc')->select();
            foreach($children as $kk=>$vv){
                $vv['text']=$vv['city'];
                $vv['value']=$list[$k]['code'].'-'.$vv['code'];
                $childrens=Area::where(['level'=>3,'parent_code'=>$vv['code']])->field('level,id,district,parent_code,code')->order('code asc')->select();
                foreach($childrens as $k3 => $v3){
                    $v3['text']=$v3['district'];
                    $v3['value']=$list[$k]['code'].'-'.$children[$kk]['code'].'-'.$v3['code'];
                    $childrens[$k3]=$v3;
                }
                $vv['children']=$childrens;
                $children[$kk]=$vv;
            }
            $v['children']=$children;
            $list[$k]=$v;
        }
        return Result::success('获取数据成功！', $list);
    }
    //设置默认收货地址
    public function chooseDefault(): Json
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            return Result::fastReturn(AddressModel::chooseDefault($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

    }
    //选择收货地址
    public function choice()
    {
        return Result::fastReturn(AddressModel::choice($this->request->post('id'), $this->user));
    }

}

